﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "GnufModelViewVisibilityFilter.h"
#include "GnufModelViewVisibilityFilterNames.h"
#include "GnufModelViewVisibilityFilterNewUtils.h"
#include "IDocument.h"
#include "IElementVisibilityFilter.h"
#include "IElementVisibilityFilterManager.h"
#include "IModelView.h"
#include "UniIdentity.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace gfam;

OwnerPtr<IElementVisibilityFilter> GnufModelViewVisibilityFilterNewUtils::CreateModelViewVisibilityFilterNew()
{
    return TransferOwnership(NEW_AS_OWNER_PTR(ModelViewVisibilityFilter));
}

const IElementVisibilityFilter* GnufModelViewVisibilityFilterNewUtils::GetVisibilityFilter(gcmp::IDocument* pDoc)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空", L"paramodel", L"2024-02-20");

    const IElementVisibilityFilterManager* pVisibilityFiletrMgr = pDoc->GetElementVisibilityFilterManager();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pVisibilityFiletrMgr, L"pVisibilityFiletrMgr文档为空", L"paramodel", L"2024-02-20");
    const IElementVisibilityFilter* pVisibilityFiletr = pVisibilityFiletrMgr->GetElementVisibilityFilter(GNUF_MODEL_VIEW_VISIBILITY_FILTER);
    return pVisibilityFiletr;
}

bool GnufModelViewVisibilityFilterNewUtils::AddVisibilityFilter(gcmp::IDocument* pDoc)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilterManager* pVisibilityFiletrMgr = pDoc->GetElementVisibilityFilterManager();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pVisibilityFiletrMgr, L"pVisibilityFiletrMgr为空", L"paramodel", L"2024-02-20");

    OwnerPtr<IElementVisibilityFilter> opFilter;
    if (!pVisibilityFiletrMgr->GetElementVisibilityFilter(GNUF_MODEL_VIEW_VISIBILITY_FILTER))
    {
        opFilter = CreateModelViewVisibilityFilterNew();
    }
    DBG_WARN_AND_RETURN_FALSE_UNLESS(opFilter, L"opFilter为空", L"paramodel", L"2024-02-20");

    bool bOk = pVisibilityFiletrMgr->AddElementVisibilityFilter(TransferOwnership(opFilter));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bOk, L"AddElementVisibilityFilter失败", L"paramodel", L"2024-02-20");

    return true;
}

bool GnufModelViewVisibilityFilterNewUtils::AddCategoryToFilter(gcmp::IDocument* pDoc, const gcmp::ElementId& modelViewId, const gcmp::UniIdentity& uid)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilterManager* pVisibilityFiletrMgr = pDoc->GetElementVisibilityFilterManager();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pVisibilityFiletrMgr, L"pVisibilityFiletrMgr文档为空", L"paramodel", L"2024-02-20");
   
    IElementVisibilityFilter* pFilter = pVisibilityFiletrMgr->GetElementVisibilityFilter(GNUF_MODEL_VIEW_VISIBILITY_FILTER);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pFilter, L"pFilter文档为空", L"paramodel", L"2024-02-20");

    ModelViewVisibilityFilter *pNewFilter = quick_cast<ModelViewVisibilityFilter>(pFilter);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pNewFilter, L"pNewFilter文档为空", L"paramodel", L"2024-02-20");
    pNewFilter->AddCategory(modelViewId, uid);

    return true;
}

bool gfam::GnufModelViewVisibilityFilterNewUtils::AddStandardCodeToFilter(gcmp::IDocument* pDoc, const gcmp::ElementId& modelViewId, const std::wstring& standardCode)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilterManager* pVisibilityFiletrMgr = pDoc->GetElementVisibilityFilterManager();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pVisibilityFiletrMgr, L"pVisibilityFiletrMgr文档为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilter* pFilter = pVisibilityFiletrMgr->GetElementVisibilityFilter(GNUF_MODEL_VIEW_VISIBILITY_FILTER);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pFilter, L"pFilter文档为空", L"paramodel", L"2024-02-20");

    ModelViewVisibilityFilter *pNewFilter = quick_cast<ModelViewVisibilityFilter>(pFilter);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pNewFilter, L"pNewFilter文档为空", L"paramodel", L"2024-02-20");
    return pNewFilter->AddHiddenStandardCode(modelViewId, standardCode);
}

bool gfam::GnufModelViewVisibilityFilterNewUtils::RemoveStandardCodeFromFilter(gcmp::IDocument* pDoc, const gcmp::ElementId& modelViewId, const std::wstring& standardCode)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilterManager* pVisibilityFiletrMgr = pDoc->GetElementVisibilityFilterManager();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pVisibilityFiletrMgr, L"pVisibilityFiletrMgr文档为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilter* pFilter = pVisibilityFiletrMgr->GetElementVisibilityFilter(GNUF_MODEL_VIEW_VISIBILITY_FILTER);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pFilter, L"pFilter文档为空", L"paramodel", L"2024-02-20");

    ModelViewVisibilityFilter *pNewFilter = quick_cast<ModelViewVisibilityFilter>(pFilter);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pNewFilter, L"pNewFilter文档为空", L"paramodel", L"2024-02-20");
    return pNewFilter->RemoveHiddenStandardCode(modelViewId, standardCode);
}

bool gfam::GnufModelViewVisibilityFilterNewUtils::IsStandardCodeExistInFilter(gcmp::IDocument* pDoc, const gcmp::ElementId& modelViewId, const std::wstring& standardCode)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilterManager* pVisibilityFiletrMgr = pDoc->GetElementVisibilityFilterManager();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pVisibilityFiletrMgr, L"pVisibilityFiletrMgr文档为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilter* pFilter = pVisibilityFiletrMgr->GetElementVisibilityFilter(GNUF_MODEL_VIEW_VISIBILITY_FILTER);
    if (!pFilter)
        return false;

    ModelViewVisibilityFilter *pNewFilter = quick_cast<ModelViewVisibilityFilter>(pFilter);
    if (!pNewFilter)
        return false;

    return pNewFilter->IsStandardCodeExist(modelViewId, standardCode);
}

bool GnufModelViewVisibilityFilterNewUtils::RemoveCategoryFromFilter(gcmp::IDocument* pDoc, const gcmp::ElementId& modelViewId, const gcmp::UniIdentity& uid)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilterManager* pVisibilityFiletrMgr = pDoc->GetElementVisibilityFilterManager();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pVisibilityFiletrMgr, L"pVisibilityFiletrMgr文档为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilter* pFilter = pVisibilityFiletrMgr->GetElementVisibilityFilter(GNUF_MODEL_VIEW_VISIBILITY_FILTER);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pFilter, L"pFilter文档为空", L"paramodel", L"2024-02-20");

    ModelViewVisibilityFilter *pNewFilter = quick_cast<ModelViewVisibilityFilter>(pFilter);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pNewFilter, L"pNewFilter文档为空", L"paramodel", L"2024-02-20");
    pNewFilter->RemoveCategory(modelViewId, uid);

    return true;
}

bool GnufModelViewVisibilityFilterNewUtils::IsCategoryExistInFilter(gcmp::IDocument* pDoc, const gcmp::ElementId& modelViewId, const gcmp::UniIdentity& uid)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilterManager* pVisibilityFiletrMgr = pDoc->GetElementVisibilityFilterManager();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pVisibilityFiletrMgr, L"pVisibilityFiletrMgr文档为空", L"paramodel", L"2024-02-20");

    IElementVisibilityFilter* pFilter = pVisibilityFiletrMgr->GetElementVisibilityFilter(GNUF_MODEL_VIEW_VISIBILITY_FILTER);
    if (!pFilter)
        return false;

    ModelViewVisibilityFilter *pNewFilter = quick_cast<ModelViewVisibilityFilter>(pFilter);
    if (!pNewFilter)
        return false;

    return pNewFilter->IsCategoryExist(modelViewId, uid);
}

